package com.example.teamtribe.main.club.moment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.database.Posting;
import com.example.teamtribe.main.club.post.PostActivity;
import com.example.teamtribe.main.club.posting.PostingActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MomentActivity extends BaseActivity implements MomentContract.View {

    public static final int MODE_TEAM = 0;
    public static final int MODE_USER = 1;
    public static final int REQUEST_ADD = 1;
    public static final int REQUEST_CHECK = 2;

    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private MomentAdapter adapter;
    private MomentContract.Presenter presenter;
    private List<Posting> data = new ArrayList<>();
    private int mode;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moment);
        mode = getIntent().getIntExtra("mode", -1);
        id = getIntent().getIntExtra("id", 0);
        presenter = new MomentPresenter(this);
        recyclerView = findViewById(R.id.rv_post_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MomentAdapter(data, id);
        recyclerView.setAdapter(adapter);
        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                presenter.start(mode, id);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                presenter.getPosts(mode, id);
            }
        });

        FloatingActionButton fab = findViewById(R.id.fab_add);
        if (mode == MODE_TEAM) {
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MomentActivity.this, PostActivity.class);
                    intent.putExtra("teamId", id);
                    startActivityForResult(intent, REQUEST_ADD);
                }
            });
        } else {
            fab.setVisibility(View.GONE);
        }
        fab.setVisibility(View.GONE);

        TextView tvTitle = findViewById(R.id.tv_title);
        if (mode == MODE_TEAM) {
            if (id == 0)
                tvTitle.setText("社团论坛");
            else
                tvTitle.setText("社团动态");
        } else {
            tvTitle.setText("我的动态");
        }
        presenter.start(mode, id);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ADD && resultCode == RESULT_OK) {
            refreshLayout.autoRefresh();
        }
        if (requestCode == REQUEST_CHECK && resultCode == PostingActivity.DELETE) {
            this.data.remove(data.getParcelableExtra("posting"));
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter.notifyDataSetChanged();
                }
            });
        }
        if (requestCode == REQUEST_CHECK && resultCode == PostingActivity.MODIFY) {
            Posting posting = data.getParcelableExtra("posting");
            this.data.remove(posting);
            ArrayList<Posting> arrayList = new ArrayList<>(1);
            arrayList.add(posting);
            addPosts(arrayList);
        }
    }

    @Override
    public void addPosts(List<Posting> postings) {
        data.addAll(postings);
        Collections.sort(data);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void clearPosts() {
        data.clear();
    }

    @Override
    public void refresh() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                refreshLayout.autoRefresh();
            }
        });
    }

    @Override
    public void complete() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                refreshLayout.finishRefresh();
                refreshLayout.finishLoadMore();
            }
        });
    }

}
